package com.wsc.wisdomchinaqinghe.activitity.fourmodule;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps2d.model.LatLng;
import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.HoveringScrollview;
import com.stx.xhb.mylibrary.XBanner;
import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.OpenExternalMapAppUtils;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.Login_RegisterActivity;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.application.MyApplication;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.constant.Constant;
import com.wsc.wisdomchinaqinghe.javaBean.FloorActivityDetailBean;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;
import com.wsc.wisdomchinaqinghe.view.iphoneDialogBuilder;

/**
 * 楼宇活动详情页面
 * Created by ${wangtao} on 2017/3/6.
 */

public class FloorEventDetailActivity extends BaseActivity implements XBanner.XBannerAdapter,View.OnClickListener,ContributorView{

    private XBanner fragment_lyhdxq_xBanner;//轮播
    private TextView fragment_lyhdxq_title;//标题
    private TextView fragment_lyhdxq_time;//时间
    private TextView fragment_lyhdxq_addres;//地址
    private WebView webView;
    private ImageView fragment_lyhdxq_back;//返回按钮
    private Button fragment_lyhdxq_enter;//立即报名
    private Context context;
    private String bid;
    private ContributorPresenter contributorPresenter;
    private final String BUILDING_ACTIVITY_Detail_FLAG="BUILDING_ACTIVITY_Detail_FLAG";
    private FloorActivityDetailBean floorActivityDetailBean;
    private MyToast myToast;
    private MyApplication myApplication;
    private boolean isDelte=false;
    private String cid;
    private int type;
    private LinearLayout ll_mine_nav;
    private LinearLayout ll_mine_nav_replace;
    private RelativeLayout layout_nav_back;
    private HoveringScrollview refreshscrollView;
    private int layout_headTop;
    private LinearLayout ll_item;
    private TextView tv_headtitle;
    private LinearLayout layout_address;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.flooreventdetail_layout);
        context=this;
        myToast=new MyToast(context);
        myApplication=(MyApplication) getApplication();
        initView();
        bid=getIntent().getStringExtra("bid");
        cid=getIntent().getStringExtra("cid");
        type=getIntent().getIntExtra("type",0);
        contributorPresenter=new ContributorPresenter(this,context);
        contributorPresenter.getBuildingActivityDetail(bid,BUILDING_ACTIVITY_Detail_FLAG);
        isDelte=getIntent().getBooleanExtra("delete",false);
        if(isDelte)
        {
            fragment_lyhdxq_enter.setVisibility(View.GONE);
        }else
        {
            fragment_lyhdxq_enter.setVisibility(View.VISIBLE);
        }

        refreshscrollView.setOnScrollListener(new HoveringScrollview.OnScrollListener() {
            @Override
            public void onScroll(int scrollY) {
                layout_headTop = ll_mine_nav_replace.getTop() + context.getResources().getDimensionPixelOffset(R.dimen.fivedp);;
                int commentTop = ll_item.getTop()/2;
                if (scrollY > layout_headTop) {
                    if (layout_nav_back.getParent() != ll_mine_nav) {
                        ll_mine_nav_replace.removeView(layout_nav_back);
                        ll_mine_nav.addView(layout_nav_back);
                        tv_headtitle.setText("活动详情");
                    }
                } else {

                    if (layout_nav_back.getParent() != ll_mine_nav_replace) {
                        ll_mine_nav.removeView(layout_nav_back);
                        ll_mine_nav_replace.addView(layout_nav_back);
                        tv_headtitle.setText("活动详情");
                    }
                }

                int alph = (int) (Tools.div(255, commentTop) * scrollY);
                if (alph <= 255) {
                    tv_headtitle.setTextColor(Color.argb(alph, 255, 255, 255));
                    fragment_lyhdxq_back.setAlpha(alph);
                    layout_nav_back.setAlpha((float) Tools.div((Tools.div(255, commentTop) * scrollY),255));
                } else {
                    tv_headtitle.setTextColor(Color.argb(255, 255, 255, 255));
                    fragment_lyhdxq_back.setAlpha(255);
                    layout_nav_back.setAlpha(1);
                }
            }
        });
    }

    private  void initView()
    {
        fragment_lyhdxq_xBanner=(XBanner) findViewById(R.id.fragment_lyhdxq_xBanner);//轮播
        fragment_lyhdxq_title=(TextView) findViewById(R.id.fragment_lyhdxq_title);//标题
        fragment_lyhdxq_time=(TextView) findViewById(R.id.fragment_lyhdxq_time);//时间
        fragment_lyhdxq_addres=(TextView) findViewById(R.id.fragment_lyhdxq_addres);//地址
        webView = (WebView) findViewById(R.id.fragment_lyhdxq_HDcontent);
        fragment_lyhdxq_back=(ImageView) findViewById(R.id.fragment_lyhdxq_back);//返回按钮
        fragment_lyhdxq_enter=(Button) findViewById(R.id.fragment_lyhdxq_enter);//立即报名
        fragment_lyhdxq_back.setOnClickListener(this);
        fragment_lyhdxq_enter.setOnClickListener(this);
        ll_mine_nav = (LinearLayout)findViewById(R.id.ll_mine_nav);
        ll_mine_nav_replace = (LinearLayout)findViewById(R.id.ll_mine_nav_replace);
        layout_nav_back = (RelativeLayout) findViewById(R.id.layout_nav_back);
        layout_nav_back.setAlpha(0);
        refreshscrollView = (HoveringScrollview) findViewById(R.id.mineScrollView);
        ll_item = (LinearLayout) findViewById(R.id.ll_item);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        layout_address=(LinearLayout) findViewById(R.id.layout_address);
        layout_address.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.fragment_lyhdxq_back:
                finish();
                break;
            case R.id.fragment_lyhdxq_enter:
                if(floorActivityDetailBean!=null)
                {
                    int flag=floorActivityDetailBean.getState();
                    if(flag==1)
                    {
                        myToast.showShort("活动未开始");
                    }else if(flag==0)
                    {
                        boolean isLogin=Tools.getSharedPreferencesboolen(Constant.isLoginIn,false,context);
                        if(isLogin)
                        {
                            Intent intent=new Intent(context,FloorEventPublishActivity.class);
                            intent.putExtra("id",floorActivityDetailBean.getId());
                            intent.putExtra("cid",cid);
                            intent.putExtra("type",type);
                            startActivity(intent);
                        }else
                        {
                            iphoneDialogBuilder idb = new iphoneDialogBuilder(context);
                            idb.setTitle("提示");
                            idb.setMessage("是否登录");
                            idb.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent(context, Login_RegisterActivity.class);
                                    myApplication.setSourceIntoLoginRegisterActivity(FloorEventDetailActivity.class.getSimpleName());
                                    startActivity(intent);
                                }
                            });
                            idb.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {

                                public void onClick(DialogInterface dialog, int which) {

                                }
                            });
                            idb.show();
                        }
                    }else
                    {
                        myToast.showShort("活动已结束");
                    }
                }
                break;
            case R.id.layout_address:
                try {
                    LatLng starLng=new LatLng(myApplication.getLatitude(),myApplication.getLongitude());
                    OpenExternalMapAppUtils.openMapDirection(this, starLng.longitude+"", starLng.latitude+"",
                            myApplication.getLocationAddress(),
                            "", "", fragment_lyhdxq_addres.getText().toString().trim(), "智慧中国");
                }catch (Exception e)
                {

                }
                break;
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(BUILDING_ACTIVITY_Detail_FLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    floorActivityDetailBean=JsonTools.jsonStringToObject(jsonMessage,FloorActivityDetailBean.class);
                    fragment_lyhdxq_title.setText(floorActivityDetailBean.getTitle());
                    fragment_lyhdxq_addres.setText(floorActivityDetailBean.getAddr());

                    String Url = Constant.Head_string + floorActivityDetailBean.getContent() + Constant.foot_string;//URL编码数据
                    webView.getSettings().setDefaultTextEncodingName("UTF-8");
                    webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                    webView.getSettings().setLoadWithOverviewMode(false);
                    webView.loadData(Url, "text/html; charset=UTF-8", null);

                    String Begin_date = Tools.timedate(String.valueOf(floorActivityDetailBean.getBeginDate()/1000));
                    String End_date = Tools.timedate(String.valueOf(floorActivityDetailBean.getEndData()/1000));
                    fragment_lyhdxq_time.setText(Begin_date+"~"+End_date);
                    fragment_lyhdxq_xBanner.setData(floorActivityDetailBean.getBanners(),null);
                    fragment_lyhdxq_xBanner.setPoinstPosition(XBanner.CENTER);//指示器居于中
                    fragment_lyhdxq_xBanner.setmAdapter(this);
                }
            }
        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }


    @Override
    public void loadBanner(com.stx.xhb.mylibrary.XBanner banner, Object model, View view, int position) {
        try{
            Glide.with(context).load(floorActivityDetailBean.getBanners().get(position)).into((ImageView) view);
        }catch (Exception e)
        {

        }
    }
}
